package com.live.adapter;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.live.activity.LiveFriendHomeActivity;
import com.live.activity.R;
import com.live.common.Constant;
import com.live.common.ImageDownloader;
import com.live.task.LiveFriend;
import com.live.tools.LiveTools;

public class LiveInfoFriendSA extends SimpleAdapter {

	public List<Map<String, Object>> list;
	public Context context;
	public int index = 0;
	public List<Map<String, Object>> items;
	private LinearLayout tabView;

	private final ImageDownloader imageDownloader = new ImageDownloader();

	public LiveInfoFriendSA(Context context, List<Map<String, Object>> items, int resource, String[] from, int[] to,
			LinearLayout tabView) {
		super(context, items, resource, from, to);
		this.list = items;
		this.context = context;
		this.items = items;
		this.tabView = tabView;
	}

	@Override
	@SuppressWarnings("unchecked")
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.info_friend_list, null, true);
		}
		if (position <= list.size()) {
			final Map<String, Object> obj = (Map<String, Object>) getItem(position);

			// 设置名字
			TextView userId = (TextView) convertView.findViewById(R.id.userId);
			userId.setText(Html.fromHtml("<font color='#2D86B0'>" + obj.get("userId") + "</font>"));

			// 设置日期
			TextView updateDate = (TextView) convertView.findViewById(R.id.updateDate);
			updateDate.setText((String) obj.get("updateDate"));

			// 设置头像
			ImageView userPic = (ImageView) convertView.findViewById(R.id.userPic);
			if (Constant.Y.equals(obj.get("userPic"))) {
				String imageUrl = Constant.SITE_URL + Constant.SITE_AVATAR_PATH + obj.get("uId") + ".jpg";
				imageDownloader.download(imageUrl, userPic, (String)obj.get("userPicVer"));
			} else {
				userPic.setImageResource(R.drawable.default_pic);
			}
			userPic.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setClass(context, LiveFriendHomeActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString("f", (String) obj.get("f"));
					bundle.putString("uId", (String) obj.get("uId"));
					bundle.putString("userId", (String) obj.get("userId"));
					bundle.putString("userSign", (String) obj.get("userSign"));
					bundle.putString("userSex", (String) obj.get("userSex"));
					bundle.putString("userAge", (String) obj.get("userAge"));
					bundle.putString("userCity", (String) obj.get("userCity"));
					bundle.putString("userPic", (String) obj.get("userPic"));
					bundle.putString("userPicVer", (String) obj.get("userPicVer"));
					intent.putExtras(bundle);
					context.startActivity(intent);
				}
			});

			// 设置拒绝按钮
			Button denialBtn = (Button) convertView.findViewById(R.id.denialBtn);
			denialBtn.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					String url = Constant.SITE_URL + "live_friend.php?locale=" + LiveTools.getLocaleLanguage() + "&";
					url += "fId=" + (String) obj.get("fId") + "&fState=" + Constant.ACTION_REMOVE;
					Bundle bundle = new Bundle();
					bundle.putString("action", Constant.ACTION_REMOVE);
					bundle.putInt("index", position);
					LiveFriend task = new LiveFriend(context, url, list, LiveInfoFriendSA.this, null, tabView, null,
							null);
					task.execute(bundle);
				}
			});

			// 设置同意按钮
			Button agreeBtn = (Button) convertView.findViewById(R.id.agreeBtn);
			agreeBtn.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					String url = Constant.SITE_URL + "live_friend.php?locale=" + LiveTools.getLocaleLanguage() + "&";
					url += "fId=" + (String) obj.get("fId") + "&fState=" + Constant.ACTION_ALLOW;
					Bundle bundle = new Bundle();
					bundle.putString("action", Constant.ACTION_REMOVE);
					bundle.putInt("index", position);
					LiveFriend task = new LiveFriend(context, url, list, LiveInfoFriendSA.this, null, tabView, null,
							null);
					task.execute(bundle);
				}
			});
		}
		return convertView;
	}

	public ImageDownloader getImageDownloader() {
		return imageDownloader;
	}
}